package com.taiyi.renaissancemall.ui.mine.order.ask_every_one;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.liaoinstan.springview.container.DefaultFooter;
import com.liaoinstan.springview.container.DefaultHeader;
import com.liaoinstan.springview.widget.SpringView;
import com.taiyi.renaissancemall.R;
import com.taiyi.renaissancemall.Utils.ToastUtils;
import com.taiyi.renaissancemall.base.BaseActivity;
import com.taiyi.renaissancemall.bean.AskEveryoneListBean;
import com.taiyi.renaissancemall.ui.mine.order.ask_every_one.answer.AnswerListActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

//问大家列表 可提问(4-40)
public class AskEveryoneListActivity extends BaseActivity<AskEveryoneListView, AskEveryoneListPresent> implements AskEveryoneListView, SpringView.OnFreshListener {

    @BindView(R.id.backIv)
    ImageView backIv;
    @BindView(R.id.titleTv)
    TextView titleTv;
    @BindView(R.id.rightIv)
    ImageView rightIv;
    @BindView(R.id.rcl_ask)
    RecyclerView rclAsk;
    @BindView(R.id.springView)
    SpringView springView;
    @BindView(R.id.edt_ask)
    EditText edtAsk;
    @BindView(R.id.btn_commit_ask)
    Button btnCommitAsk;
    @BindView(R.id.top)
    ConstraintLayout top;
    @BindView(R.id.noDataIv)
    ImageView noDataIv;
    private int page = 1;
    private String commodityid;
    private List<AskEveryoneListBean.WelistBean> data = new ArrayList<>();
    private AskEveryoneListAdapter askEveryoneListAdapter;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_ask_everyone;
    }

    @Override
    public AskEveryoneListPresent initPresenter() {
        return new AskEveryoneListPresent();
    }

    @Override
    protected void initViews(Bundle savedInstanceState) {
        commodityid = getIntent().getStringExtra("commodityid");

        titleTv.setText(getString(R.string.askEveryone));

        springView.setHeader(new DefaultHeader(this));
        springView.setFooter(new DefaultFooter(this));
        springView.setGive(SpringView.Give.BOTH);
        springView.setType(SpringView.Type.FOLLOW);
        springView.setListener(this);

        setAdapter(data);

        if (askEveryoneListAdapter != null) {
            askEveryoneListAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                    Intent intent = new Intent(AskEveryoneListActivity.this, AnswerListActivity.class);
                    intent.putExtra("commodityid", commodityid);
                    intent.putExtra("position", position);
                    startActivity(intent);
                }
            });
        }
    }

    private void setAdapter(List<AskEveryoneListBean.WelistBean> welistBeans) {
        askEveryoneListAdapter = new AskEveryoneListAdapter(R.layout.layout_ask_list, welistBeans);
        rclAsk.setLayoutManager(new LinearLayoutManager(AskEveryoneListActivity.this, LinearLayoutManager.VERTICAL, false));
        rclAsk.setAdapter(askEveryoneListAdapter);
    }

    @Override
    protected void initImmersionBar() {
        super.initImmersionBar();
        mImmersionBar.titleBar(top).init();
    }

    @Override
    protected void initData() {
        showProgress();
        presenter.getAskList(page + "", commodityid);
    }

    @Override
    public void initEvent() {

    }

    @Override
    public void getAskListSuccess(AskEveryoneListBean askEveryoneListBean) {
        hideProgress();
        springView.onFinishFreshAndLoad();
        if (page == 1) {
            data.clear();
            if (askEveryoneListBean.welist == null || askEveryoneListBean.welist.size() == 0) {
                noDataIv.setVisibility(View.VISIBLE);
            } else {
                noDataIv.setVisibility(View.GONE);
            }
        }
        data.addAll(askEveryoneListBean.welist);
        if (askEveryoneListAdapter != null) {
            askEveryoneListAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void getAskListFailure(String msg) {
        hideProgress();
        springView.onFinishFreshAndLoad();
        ToastUtils.showShortToast(msg);
    }

    @Override
    public void addProblemSuccess(String msg_cn) {
        ToastUtils.showShortToast(msg_cn);
        edtAsk.setText("");
        page = 1;
        presenter.getAskList(page + "", commodityid);
    }

    @Override
    public void addProblemFailure(String msg) {
        hideProgress();
        ToastUtils.showShortToast(msg);
    }

    @OnClick({R.id.backIv, R.id.btn_commit_ask})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.backIv:
                finish();
                break;
            case R.id.btn_commit_ask:
                String add_problem = edtAsk.getText().toString().trim();
                if (add_problem.length() < 4 || add_problem.length() > 40) {
                    ToastUtils.showShortToast("请输入提问(4-40)");
                    return;
                }
                showProgress();
                presenter.addProblem(commodityid, add_problem);
                break;
        }
    }

    @Override
    public void onRefresh() {
        page = 1;
        presenter.getAskList(page + "", commodityid);
    }

    @Override
    public void onLoadmore() {
        page++;
        presenter.getAskList(page + "", commodityid);
    }
}
